class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size() && inorder.size()){
            TreeNode* root = new TreeNode(preorder[0]);
            int i=0;
            for(i=0;i<inorder.size();i++){
                if(inorder[i] == preorder[0]){
                    break;
                }
            }
            preorder.erase(preorder.begin());
            vector<int> left_in(inorder.begin(),inorder.begin() + i);
            root->left = buildTree(preorder,left_in);
            vector<int> right_in(inorder.begin()+i+1,inorder.end());
            root->right = buildTree(preorder,right_in);
            return root;
        }
        return nullptr;
    }
};